package com.yalantis.taurus.library;


import ohos.agp.animation.Animator;
import ohos.agp.components.*;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import java.util.Date;

public class RefreshView extends DirectionalLayout implements BaseRefreshHeader {
    private static final String XR_REFRESH_KEY = "XR_REFRESH_KEY";
    private static final String XR_REFRESH_TIME_KEY = "XR_REFRESH_TIME_KEY";
    private static final int ROTATE_ANIM_DURATION = 180;

    private DirectionalLayout mContainer;
    private Component mComponent;
    private Animator mRotateUpAnim;
    private Animator mRotateDownAnim;
    private int mState = STATE_NORMAL;
    public int mMeasuredHeight;
    private DirectionalLayout layout;
    private ContinuousScrollableImageView plane;

    public RefreshView(Context context) {
        this(context,null,null);
    }

    public RefreshView(Context context, AttrSet attrSet) {
        this(context, attrSet,null);
    }

    public RefreshView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context, attrSet, styleName);
    }

    private void initView(Context context, AttrSet attrSet, String styleName) {
        LayoutScatter scatter = LayoutScatter.getInstance(context);
        mContainer = (DirectionalLayout) scatter.parse(ResourceTable.Layout_listview_header_custom, null, false);

        LayoutConfig lp = new LayoutConfig(LayoutConfig.MATCH_PARENT,LayoutConfig.MATCH_CONTENT);
        lp.setMargins(0,0,0,0);
        this.setPadding(0,0,0,0);
        this.setLayoutConfig(lp);

        addComponent(mContainer,new LayoutConfig(LayoutConfig.MATCH_PARENT,0));
        setAlignment(TextAlignment.BOTTOM);

        layout = (DirectionalLayout) mContainer.findComponentById(ResourceTable.Id_layout);
        LayoutConfig planeConfig=new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,0,LayoutConfig.UNSPECIFIED_ALIGNMENT,1);
        plane=new ContinuousScrollableImageView(context);
        plane.setLayoutConfig(planeConfig);
        plane.setDirection(ContinuousScrollableImageView.RIGHT);
        plane.setDuration(800);
        plane.setScaleType(ContinuousScrollableImageView.CENTER_INSIDE);
        plane.setResourceId(ResourceTable.Media_airplane);
        layout.addComponent(plane);
        estimateSize(ComponentContainer.LayoutConfig.MATCH_CONTENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mMeasuredHeight = getEstimatedHeight();
    }
    public void setState(int state) {
        if(state == mState){
            return;
        }

        if(state == STATE_REFRESHING){// 显示进度
            plane.setViewStart();
            smoothScrollTo(mMeasuredHeight);
        }else if(state == STATE_DONE){
            plane.setViewStop();
        }else {// 显示箭头图片
//            mArrowImageView.setVisibility(VISIBLE);
//            if(mProgressBar != null){
//                mProgressBar.setVisibility(INVISIBLE);
//            }
        }

//        mHeaderTimeView.setText(friendlyTime(getLastRefreshTime()));
        switch (state){
            case STATE_NORMAL:
                if (mState == STATE_RELEASE_TO_REFRESH) {
//                    mArrowImageView.startAnimation(mRotateDownAnim);
                }
                if (mState == STATE_REFRESHING) {
//                    mArrowImageView.clearAnimation();
                }
//                mArrowImageView.setRotation(0);
//                mStatusTextView.setText("下拉刷新");
                break;
            case STATE_RELEASE_TO_REFRESH:
                if (mState != STATE_RELEASE_TO_REFRESH) {
//                    mArrowImageView.setRotation(180);
//                    mStatusTextView.setText("释放立即刷新");
                }
                break;
            case STATE_REFRESHING:
//                mStatusTextView.setText("正在刷新...");
                break;
            case STATE_DONE:
//                mStatusTextView.setText("刷新完成");
                break;
            default:
                break;
        }
        mState = state;
    }

    public int getState() {
        return mState;
    }



    @Override
    public void refreshComplete() {
        setState(STATE_DONE);
        reset();
    }

    public void setVisibleHeight(int height){
        if(height <= 0){
            height = 0;
        }else if(height >400){
            height = 400;
        }
        LayoutConfig lp = (LayoutConfig)mContainer.getLayoutConfig();
        lp.height = height;
        mContainer.setLayoutConfig(lp);
    }

    public int getVisibleHeight() {
        LayoutConfig lp = (LayoutConfig)mContainer.getLayoutConfig();
        return lp.height;
    }

    @Override
    public void onMove(float delta) {
        if(getVisibleHeight() > 0 || delta > 0){
            setVisibleHeight((int)delta);
            if(mState <= STATE_RELEASE_TO_REFRESH){// 未处于刷新状态，更新箭头
                if (getVisibleHeight() > mMeasuredHeight) {
                    setState(STATE_RELEASE_TO_REFRESH);
                }else {
                    setState(STATE_NORMAL);
                }
            }
        }
    }

    @Override
    public boolean releaseAction() {
        boolean isOnRefresh = false;
        int height = getVisibleHeight();
        if(height ==0){
            isOnRefresh = false;
        }

        if(getVisibleHeight() > mMeasuredHeight && mState < STATE_REFRESHING){
            setState(STATE_REFRESHING);
            isOnRefresh = true;
        }
        if (mState != STATE_REFRESHING) {
            smoothScrollTo(0);
        }

        if (mState == STATE_REFRESHING) {
            int destHeight = mMeasuredHeight;
            setVisibleHeight(destHeight);
        }

        return isOnRefresh;
    }

    public void reset(){
        smoothScrollTo(0);
        setState(STATE_NORMAL);
    }

    private void smoothScrollTo(int destHeight) {
        setVisibleHeight(0);
        postLayout();
        invalidate();
    }

    public static String friendlyTime(Date time) {
        return friendlyTime(time.getTime());
    }

    public static String friendlyTime(long time) {
        //获取time距离当前的秒数
        int ct = (int)((System.currentTimeMillis() - time)/1000);

        if(ct == 0) {
            return "刚刚";
        }

        if(ct > 0 && ct < 60) {
            return ct + "秒前";
        }

        if(ct >= 60 && ct < 3600) {
            return Math.max(ct / 60,1) + "分钟前";
        }
        if(ct >= 3600 && ct < 86400)
            return ct / 3600 + "小时前";
        if(ct >= 86400 && ct < 2592000){ //86400 * 30
            int day = ct / 86400 ;
            return day + "天前";
        }
        if(ct >= 2592000 && ct < 31104000) { //86400 * 30
            return ct / 2592000 + "月前";
        }
        return ct / 31104000 + "年前";
    }
}
